package com.hc.sort.take;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import javax.swing.*;
import javax.swing.border.*;

import com.hc.sort.server.pojo.BusinessType;
import com.hc.sort.server.pojo.Ticket;
import com.hc.sort.utils.NetClient;

public class TakeUI extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TakeUI frame = new TakeUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TakeUI() {
		setTitle("取号系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 638, 536);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//获取业务列表，并显示在界面上
		List<BusinessType> businessList=getBusinessList();
		System.out.println(businessList.size());
		System.out.println(businessList);
		for (int i = 0; i < businessList.size(); i++) {
			BusinessType businessType=businessList.get(i);
			//使用带服务类型编码的按钮类型创建按钮
			MyServiceButton vipBtn = new MyServiceButton(businessType.getName(),businessType.getCode());
			vipBtn.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					//调用接口来获取号码
					HashMap reqMap = new HashMap();
					reqMap.put("arg0",((MyServiceButton)e.getSource()).getServiceTypeCode());
					reqMap.put("command","quhao");
					
					HashMap retMap =(HashMap) NetClient.sendMsg(reqMap);
					
					int result =(int) retMap.get("result");
					SimpleDateFormat date1= new SimpleDateFormat("yyyy-MM-dd"); 
					if(result>=0) {
						Ticket tk =(Ticket) retMap.get("data1");
						JOptionPane.showMessageDialog(null, tk.getBusinessName()+"\r\n"
								+"号码："+tk.getNo()+" \r\n"
								+"等待人数："+tk.getWaitCount()+" \r\n"
								+"时间："+date1.format(tk.getTakeTime()));
					}else {
						JOptionPane.showMessageDialog(null,"取号失败");
					}
				}
			});
			vipBtn.setFont(new Font("宋体", Font.PLAIN, 24));
			vipBtn.setBounds(173, 88+(i-1)*90+50, 280, 66);
			contentPane.add(vipBtn);
		}
		
		JLabel labelbackground = new JLabel("");
		labelbackground.setIcon(new ImageIcon("D:\\course\\eclipse-workspace\\HuicaiBankSortSys\\src\\img\\取号背景.jpg"));
		labelbackground.setBounds(0, 0, 596, 356);
		contentPane.add(labelbackground);
	}
	
	List<BusinessType> getBusinessList(){
		HashMap reqMap=new HashMap();
		reqMap.put("command", "businessList"); //请求获取业务列表
		
		HashMap retMap=(HashMap) NetClient.sendMsg(reqMap);
		List<BusinessType> businessTypes=(List<BusinessType>) retMap.get("data1");
		return businessTypes;
	}
}
